package com.shop.milk_tea_shop.manager;

import com.shop.milk_tea_shop.dto.ItemAddDTO;
import com.shop.milk_tea_shop.entity.ItemAdd;
import com.shop.milk_tea_shop.entity.ItemAddCategory;
import com.shop.milk_tea_shop.entity.ItemInfo;
import com.shop.milk_tea_shop.request.ItemAddsQueryRequest;
import com.shop.milk_tea_shop.service.ItemAddCategoryService;
import com.shop.milk_tea_shop.service.ItemAddService;
import com.shop.milk_tea_shop.service.ItemInfoService;
import com.shop.milk_tea_shop.utils.ItemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 俞静雯
 * @Description 商品附加信息管理类
 * @date 2024-06-23  15:33
 */
@Component
public class ItemAddManager {

    @Autowired
    private ItemInfoService itemInfoService;

    @Autowired
    private ItemAddCategoryService itemAddCategoryService;

    @Autowired
    private ItemAddService itemAddService;

    /**
     * 获取并组装单个商品的附加信息列表
     * @param itemId
     * @return
     */
    public List<ItemAddDTO> getItemAdds(Long itemId) {
        ItemInfo itemInfo = itemInfoService.getById(itemId);
        Map<Long, List<Long>> additionMap = ItemUtils.parseAddition(itemInfo.getAddtion());
        Set<Long> categoryIds = additionMap.keySet();
        List<Long> addIdList = ItemUtils.getAddIdList(categoryIds, additionMap);

        Map<Long, ItemAddCategory> categoryMap = itemAddCategoryService.listByIds(categoryIds)
                .stream()
                .collect(Collectors.toMap(x -> x.getAddCategoryId(), x -> x));
        Map<Long, List<ItemAdd>> itemAddMap = itemAddService.listByIds(addIdList)
                .stream()
                .collect(Collectors.groupingBy(ItemAdd::getAddCategoryId));

        List<ItemAddDTO> result = new ArrayList<>();
        categoryIds.forEach(id -> {
            List<ItemAdd> itemAdds = itemAddMap.get(id);
            if (!itemAdds.isEmpty()) {
                ItemAddDTO itemAddDTO = new ItemAddDTO();
                itemAddDTO.setItemAddCategoryId(id);
                itemAddDTO.setItemAddCategoryName(categoryMap.get(id).getAddCategoryName());
                itemAddDTO.setAdds(itemAdds);
                result.add(itemAddDTO);
            }
        });
        return result;
    }

    /**
     * 获取并组装附加信息菜单
     * @param request
     * @return
     */
    public List<ItemAddDTO> itemAddsMenu(ItemAddsQueryRequest request) {
        List<ItemAddDTO> result = new ArrayList<>();
        List<ItemAddCategory> categoryList = itemAddCategoryService.getMenu(request);
        Map<Long, List<ItemAdd>> addsMap = itemAddService.getMenu(request)
                .stream()
                .collect(Collectors.groupingBy(ItemAdd::getAddCategoryId));
        categoryList.forEach(category -> {
            Long addCategoryId = category.getAddCategoryId();
            List<ItemAdd> itemAdds = addsMap.get(addCategoryId);
            if (!itemAdds.isEmpty()) {
                ItemAddDTO itemAddDTO = new ItemAddDTO();
                itemAddDTO.setItemAddCategoryId(addCategoryId);
                itemAddDTO.setItemAddCategoryName(category.getAddCategoryName());
                itemAddDTO.setAdds(itemAdds);
                result.add(itemAddDTO);
            }
        });
        return result;
    }
}
